package jdbc.prepareStatement;

import java.sql.*;

public class TestMetaData {
	Connection con;
	public TestMetaData() {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	//get meg of DatabaseMetaData
	public void getDmdMsg(){
		try {
//			SQLException s = new SQLException();
//			if(true){
//				throw s;
//			}
			DatabaseMetaData dmd = con.getMetaData();
			System.out.println(dmd.getDriverName()+" \n"+dmd.getDriverVersion());
			System.out.println(dmd.getDatabaseProductName()+"\n"+dmd.getDatabaseProductVersion());
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	//get msg of ResultSet MetaData
	public void getRsmd(){
		String sql = "select * from student";
		try {
			Statement sm = con.createStatement();
			ResultSet rs = sm.executeQuery(sql);
			ResultSetMetaData rsmd = rs.getMetaData();
			System.out.println(rsmd.getColumnCount());
			for(int i=1;i<=rsmd.getColumnCount();i++){
				System.out.println("column name:"+rsmd.getColumnName(i));
				System.out.println("field type:： "+rsmd.getColumnClassName(i));
				System.out.println("column num type:"+rsmd.getColumnType(i));
				System.out.println("field type of db:"+rsmd.getColumnTypeName(i));
				System.out.println("table name:"+rsmd.getTableName(i)+"\n");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	//关闭
	public void close(){
		try {
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public static void main(String[] args) {
		TestMetaData tMd = new TestMetaData();
//		tMd.getDmdMsg();
		tMd.getRsmd();
		tMd.close();
		
		
	}

}
